package com.findme.fecedetection.net

import com.findme.fecedetection.bean.*
import com.findme.fecedetection.utlis.getMD5
import com.google.gson.JsonObject
import com.tanguy.user.utlis.extend.loge
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody



/**
 * Created by dab on 2017/11/20 0020 21:04
 */
object HttpManager {
    val REQUEST_ERROR = "请求出错！"
    val PARSER_ERROR = "数据解析出错！"
    val NET_ERROR = "网络异常，请检查您的网络状态！"
    val PAGE_SIZE = 20

    fun getPage(color: String="", category: String="", type: String="", size: String="", location: String="", userCode: String="", pageNo: String=""): Observable<ResultData<ResultList<ArrayList<PetListBean>>>> {
        return RRetrofit.instance().create(ApiService::class.java).getPage(color, category, type, size, location, userCode, pageNo)

    }

    fun singleDetails(petId: Int): Observable<ResultData<PetDetailsBean>> {
        return RRetrofit.instance().create(ApiService::class.java).singleDetails(petId)
    }

    fun uploadImage(partList: List<MultipartBody.Part>): Observable<ResponseBody> {
        return RRetrofit.instance().create(ApiService::class.java).uploadImage(partList)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
    }


    fun address(): Observable<ResultData<JsonObject>> {
        return RRetrofit.instance().create(ApiService::class.java).address()
    }

    fun getCode(mobile: String): Observable<ResultData<Any?>> {
        return RRetrofit.instance().create(ApiService::class.java).getCode(mobile)
    }

    fun regist(phone: String, userPwd: String, verifyCode: String): Observable<ResultData<Any>> {
        val json=" {\n" +
                "   \"userCode\":\"$phone\",\n" +
                "   \"userPwd\":\"${getMD5(userPwd)}\",\n" +
                "   \"verifyCode\":\"$verifyCode\",\n" +
                "   \"channel\":\"1\"\n" +
                "}"
        return RRetrofit.instance().create(ApiService::class.java).regist(getBody(json))
    }

    fun login(phone: String, userPwd: String): Observable<ResultData<Any>> {
        val json="  {\n" +
                "   \"userCode\":\"$phone\",\n" +
                "   \"userPwd\":\"${getMD5(userPwd)}\"\n" +
                "  }"
        return RRetrofit.instance().create(ApiService::class.java).login(getBody(json))
    }

    fun list() : Observable<ResultData<List<Dictionaries>>>{
        return RRetrofit.instance().create(ApiService::class.java).list()
    }
    fun getBody(json: String): RequestBody {
        loge(json)
        return RequestBody.create(MediaType.parse("application/json"), json)
    }


}
